<?php

namespace App\Http\Controllers\User;

use App\Events\InfoChecked;
use App\Http\Controllers\Controller;
use App\Models\Info;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class InfoController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    /* 查看通知详情
     *
     * @param id
     * @return info
     */
    public function show(Info $info){
        $user = User::getInfoByOpenid($info->owner);
        $this->authorize('editOrder',$user);
        event(new InfoChecked($info));
        return $info;
    }

    /* 查询个人通知列表
     *
     * @param openid
     * @return info
     */
    public function list($openid, $type=3){
        $user = User::getInfoByOpenid($openid);
        $this->authorize('editOrder',$user);
        $infos = new Info();
        $infos = Info::InfoType($infos, $type);
        $infos = $infos->latest()->get();
        return $infos;
    }

    /* 删除通知
     *
     * @param id
     * @return info
     */
    public function delete(Info $info){
        $user = User::getInfoByOpenid($info->owner);
        $this->authorize('editOrder',$user);
        $info->delete();
        return $this->responseSuccess();
    }

}
